package edu.ustb.efws.base.controller;

import edu.ustb.efws.base.controller.form.AlarmSaveForm;
import edu.ustb.efws.base.controller.form.AlarmUpdateForm;
import edu.ustb.efws.base.domain.Alarm;
import edu.ustb.efws.base.domain.Position;
import edu.ustb.efws.base.domain.User;
import edu.ustb.efws.base.service.AlarmService;
import edu.ustb.efws.base.service.PositionService;
import edu.ustb.efws.base.service.UserService;
import edu.ustb.efws.controller.vo.ResultPageVo;
import edu.ustb.efws.controller.vo.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/manage/alarm")
@Api(value = "/manage/alarm", tags = "M-报警设置管理")
public class AlarmController {
    private final AlarmService service;
    private final UserService userService;
    private final PositionService positionService;

    @Autowired
    public AlarmController(AlarmService service, UserService userService, PositionService positionService) {

        this.service = service;
        this.userService = userService;
        this.positionService = positionService;
    }

    @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation("新增报警设置")
    public ResultVo<Alarm> save(@Valid @RequestBody AlarmSaveForm form){
        User user = userService.get(form.getUserId());

        Alarm t = new Alarm();
        t.setUserId(user.getId());
        t.setDdUserId(user.getDdUserId());
        t.setName(user.getName());
        t.setEnable(form.getEnable());
        List<Alarm.SimplePosition> positions = form.positionIds.stream()
                .map(positionService::get).map(this::toSimplePosition).collect(Collectors.toList());
        t.setPositions(positions);

        return ResultVo.success(service.save(t));
    }

    private Alarm.SimplePosition toSimplePosition(Position t){
        Alarm.SimplePosition s = new Alarm.SimplePosition();

        s.setId(t.getId());
        s.setRiskPosition(t.getRiskPosition());
        s.setRiskArea(t.getRiskArea());

        return s;
    }

    @PutMapping(consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation("修改报警设置")
    public ResultVo<Alarm> update(@Valid @RequestBody AlarmUpdateForm form){
        Alarm t = service.get(form.getId());
        if(!t.getUserId().equals(form.getUserId())){
            User user = userService.get(form.getUserId());
            t.setUserId(user.getId());
            t.setDdUserId(user.getDdUserId());
            t.setName(user.getName());
        }

        t.setEnable(form.getEnable());
        List<Alarm.SimplePosition> positions = form.positionIds.stream()
                .map(positionService::get).map(this::toSimplePosition).collect(Collectors.toList());
        t.setPositions(positions);

        return ResultVo.success(service.update(t));
    }

    @PutMapping(value = "open/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation("开启报警")
    public ResultVo<Alarm> open(@PathVariable("id")long id){
        return ResultVo.success(service.open(id));
    }

    @PutMapping(value = "close/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation("关闭报警")
    public ResultVo<Alarm> close(@PathVariable("id")long id){
        return ResultVo.success(service.close(id));
    }

    @GetMapping(value = "{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation("获取报警")
    public ResultVo<Alarm> get(@PathVariable("id")long id){
        return ResultVo.success(service.get(id));
    }

    @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation("查询报警")
    public ResultPageVo<Alarm> query(
            @ApiParam(value = "设备类型") @RequestParam(required = false) String name,
            @RequestParam(defaultValue = "true") @ApiParam(value = "是否查询记录数") Boolean isCount,
            @RequestParam(defaultValue = "0") @ApiParam(value = "查询页数") int page,
            @RequestParam(defaultValue = "15") @ApiParam(value = "查询每页记录数") int rows){

        return new ResultPageVo
                .Builder<>(page, rows, service.query(name, page * rows, rows))
                .count(isCount, () -> service.count(name))
                .build();
    }
}
